package nix.service.impl;

import nix.dao.UserDao;
import nix.domain.User;
import nix.service.UserService;
import nix.utils.MD5Utils;

public class UserServiceImpl implements UserService {

	private UserDao userDao;

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	
	@Override
	public User getUserByCodePassword(User user) {
	
		User u = userDao.GetByUserCode(user.getUser_code());
		if (u == null) {
			throw new RuntimeException("用户名不存在！");
		} else if (!u.getUser_password().equals(MD5Utils.md5(user.getUser_password()))) {
			throw new RuntimeException("密码错误！");
		}
		return u;
	}

	@Override
	public void saveUser(User user) {
		
		User u = userDao.GetByUserCode(user.getUser_code());
		if (u != null) {
			throw new RuntimeException("登录名已存在！");
		}
		user.setUser_password(MD5Utils.md5(user.getUser_password()));
		userDao.save(user);
	}

}
